<MDXHeader
  title="OTP Field"
  description="Accessible one-time password component with copy paste functionality."
  docs="https://corvu.dev/docs/primitives/otp-field/"
/>

<ComponentPreview name="otp-field-demo" />

## Installation

<Tabs defaultValue="cli">

<TabsList>
  <TabsTrigger value="cli">CLI</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

<Steps>

<Step>Run the following command:</Step>

```bash
npx solidui-cli@latest add otp-field
```

<Step>Update `tailwind.config.cjs`</Step>

Add the following animations to your `tailwind.config.cjs` file:

```js {6-9,12}
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      keyframes: {
        "caret-blink": {
          "0%,70%,100%": { opacity: "1" },
          "20%,50%": { opacity: "0" }
        }
      },
      animation: {
        "caret-blink": "caret-blink 1.25s ease-out infinite"
      }
    }
  }
}
```

</Steps>

</TabsContent>

<TabsContent value="manual">
<Steps>

<Step>Install the following dependencies: </Step>

```bash
npm install @corvu/otp-field
```

<Step>Copy and paste the following code into your project: </Step>

<ComponentSource name="otp-field" />

<Step>Update `tailwind.config.cjs`</Step>

Add the following animations to your `tailwind.config.cjs` file:

```js {6-9,12}
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      keyframes: {
        "caret-blink": {
          "0%,70%,100%": { opacity: "1" },
          "20%,50%": { opacity: "0" }
        }
      },
      animation: {
        "caret-blink": "caret-blink 1.25s ease-out infinite"
      }
    }
  }
}
```

</Steps>
</TabsContent>

</Tabs>

## Usage

```tsx
import {
  OTPField,
  OTPFieldGroup,
  OTPFieldInput,
  OTPFieldSeparator,
  OTPFieldSlot
} from "~/registry/ui/otp-field"
```

```tsx
<OTPField maxLength={6}>
  <OTPFieldInput />
  <OTPFieldGroup>
    <OTPFieldSlot index={0} />
    <OTPFieldSlot index={1} />
    <OTPFieldSlot index={2} />
  </OTPFieldGroup>
  <OTPFieldSeparator />
  <OTPFieldGroup>
    <OTPFieldSlot index={3} />
    <OTPFieldSlot index={4} />
    <OTPFieldSlot index={5} />
  </OTPFieldGroup>
</OTPField>
```

## Examples

### Pattern

Use the `pattern` prop to define a custom pattern for the OTP input.

```tsx
import { REGEXP_ONLY_DIGITS_AND_CHARS } from "~/components/ui/otp-field"
```

```tsx {2}
<OTPField maxLength={6}>
  <OTPFieldInput pattern={REGEXP_ONLY_DIGITS_AND_CHARS} />
  <OTPFieldGroup>
    <OTPFieldSlot index={0} />
    <OTPFieldSlot index={1} />
    <OTPFieldSlot index={2} />
  </OTPFieldGroup>
  <OTPFieldSeparator />
  <OTPFieldGroup>
    <OTPFieldSlot index={3} />
    <OTPFieldSlot index={4} />
    <OTPFieldSlot index={5} />
  </OTPFieldGroup>
</OTPField>
```
